/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.stock.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.dao.PdDao;
import com.thinkgem.jeesite.modules.stock.dao.SpdDetailDao;

/**
 * 盘点Service
 * @author 盘点
 * @version 2019-12-21
 */
@Service
@Transactional(readOnly = true)
public class PdService extends CrudService<PdDao, Pd> {

	@Autowired
	private SpdDetailDao spdDetailDao;
	
	public Pd get(String id) {
		Pd pd = super.get(id);
		pd.setSpdDetailList(spdDetailDao.findList(new SpdDetail(pd)));
		return pd;
	}
	
	public List<Pd> findList(Pd pd) {
		return super.findList(pd);
	}
	
	public Page<Pd> findPage(Page<Pd> page, Pd pd) {
		return super.findPage(page, pd);
	}

	@Autowired
	private ActTaskService actTaskService;
	@Transactional(readOnly = false)
	public void save(Pd pd) {

		if (StringUtils.isBlank(pd.getId())) {
			pd.preInsert();
			dao.insert(pd);
			// 启动流程
			actTaskService.startProcess("pandian", "s_pd", pd.getId(), "盘点审批");



		}
		else{
			pd.preUpdate();
			dao.update(pd);
			pd.getAct().setComment(("yes".equals(pd.getAct().getFlag())?"[重申] ":"[销毁] ")+"盘点审批");

			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass", "yes".equals(pd.getAct().getFlag())? "1" : "0");
			actTaskService.complete(pd.getAct().getTaskId(), pd.getAct().getProcInsId(), pd.getAct().getComment(), "盘点审批", vars);

		}
		for (SpdDetail spdDetail : pd.getSpdDetailList()){
			if (spdDetail.getId() == null){
				continue;
			}
			if (spdDetail.DEL_FLAG_NORMAL.equals(spdDetail.getDelFlag())){
				if (StringUtils.isBlank(spdDetail.getId())){
					spdDetail.setPid(pd);
					spdDetail.preInsert();
					spdDetailDao.insert(spdDetail);
				}else{
					spdDetail.preUpdate();
					spdDetailDao.update(spdDetail);
				}
			}else{
				spdDetailDao.delete(spdDetail);
			}
		}
	}

	@Autowired
	private StockService stockService;
	@Autowired
	private StockWarehouseService stockWarehouseService;
	@Transactional(readOnly = false)
	public void delete(Pd pd) {
		super.delete(pd);
		spdDetailDao.delete(new SpdDetail(pd));
	}
	@Transactional(readOnly = false)
	public void auditSave(Pd pd) {
		// 设置意见
		pd.getAct().setComment(("yes".equals(pd.getAct().getFlag())?"[同意] ":"[驳回] ")+pd.getAct().getComment());

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = pd.getAct().getTaskDefKey();
		// 审核环节
		if ("audit1".equals(taskDefKey)){

			for (SpdDetail spdDetail : pd.getSpdDetailList()){
				spdDetail.preUpdate();
				spdDetailDao.update(spdDetail);
			}
		}
		else if ("audit2".equals(taskDefKey)){


		}
		else if ("audit3".equals(taskDefKey)){

			if("yes".equals(pd.getAct().getFlag())){
				for (SpdDetail spdDetail : pd.getSpdDetailList()){
					Stock s = new Stock();
					s.setW(spdDetail.getWoods());
					List<Stock> ss=stockService.findList(s);
					if(ss!=null&&ss.size()>0){
						Stock stock2=ss.get(0);
						stock2.setNumber(stock2.getNumber()+spdDetail.getFactnumber()-spdDetail.getFactnumber());
						stockService.save(stock2);
					}
					StockWarehouse stockWarehouse=new StockWarehouse();
					stockWarehouse.setWid(spdDetail.getWoods());
					stockWarehouse.setWhid(pd.getLoc());
					List<StockWarehouse> sws=stockWarehouseService.findList(stockWarehouse);
					if(sws!=null&&sws.size()>0){
						stockWarehouse=sws.get(0);
						stockWarehouse.setNumber(stockWarehouse.getNumber()+spdDetail.getFactnumber()-spdDetail.getFactnumber());
						stockWarehouseService.save(stockWarehouse);
					}
				}
			}



		}
		else if ("audit4".equals(taskDefKey)){


		}



		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass", "yes".equals(pd.getAct().getFlag())? "1" : "0");
		actTaskService.complete(pd.getAct().getTaskId(), pd.getAct().getProcInsId(), pd.getAct().getComment(), vars);

	}
}